//package com.example.shipback.config;
//
//import com.example.shipback.utils.WebSocketUtils;
//import com.fasterxml.jackson.databind.JsonNode;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.node.ArrayNode;
//import com.fasterxml.jackson.databind.node.ObjectNode;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.ResponseEntity;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.web.client.RestTemplate;
//
//@Component
//public class Timed {
//    private static final ObjectMapper objectMapper = new ObjectMapper();
//
//    @Scheduled(cron = "*/10 * * * * ?")
//    public void everyhour() {
//        try {
//            // 1、调用MOM_DeviceStatus接口
//            RestTemplate restTemplate = new RestTemplate();
//            String url = "http://172.16.228.38:8347/api/shuziluansheng/MOM_DeviceStatus";
//            HttpHeaders headers = new HttpHeaders();
//            headers.set("Content-Type", "application/json");
//
//            // 创建请求体
//            String requestBody = "{}";
//            HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
//
//            // 发送POST请求
//            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
//            String originalResult = response.getBody();
//            System.out.println("Original MOM_DeviceStatus Response: " + originalResult);
//
//            // 2、将原始响应转换为指定格式
//            if (originalResult != null) {
//                // 解析原始响应
//                JsonNode rootNode = objectMapper.readTree(originalResult);
//                JsonNode dataNode = rootNode.path("devicestatus");
//
//                // 创建新的响应结构
//                ObjectNode newResponse = objectMapper.createObjectNode();
//                newResponse.put("api_name", "D_state");
//                ArrayNode newDataArray = objectMapper.createArrayNode();
//
//                // 检查data是否为数组
//                if (dataNode.isArray()) {
//                    for (JsonNode item : dataNode) {
//                        ObjectNode newItem = objectMapper.createObjectNode();
//                        newItem.put("DeviceID", item.path("device_id").asText());
//                        newItem.put("State", item.path("device_status").asText());
//                        newDataArray.add(newItem);
//                    }
//                }
//
//                newResponse.set("data", newDataArray);
//                String formattedResult = objectMapper.writeValueAsString(newResponse);
//                System.out.println("Formatted Response: " + formattedResult);
//
//                // 3、将格式化后的结果传递给sendMessage接口
//                String wsUrl = "ws://localhost:8094/ws";
//                WebSocketUtils.sendMessage(wsUrl, formattedResult);
//                System.out.println("Formatted message sent to WebSocket");
//            }
//        } catch (Exception e) {
//            System.err.println("Error in timed task: " + e.getMessage());
//            e.printStackTrace();
//        }
//    }
//}
